//============================================================================
// Name        : slide_path.cpp
// Author      : dfoelber
//============================================================================

// INCLUDES //
#include "physics/slide_path.h"
#include <iostream>
//////////////

using namespace std;

/**
* Constructor
* 
* @param direction The slide's direction
* @param length The length of the slide
* @param initialVel Velocity of the slide
**/
SlidePath::SlidePath( int direction, int length, double initialVel ) : 
	Path(),
	velocity(initialVel),
	direction(direction),
	length((double)length)
{
}

/**
* Lets the path know that time has passed and to move accordingly.
* 
* Overridden from Path.
* 
* @param ms The time passed since last called
**/
void SlidePath::TimePassed( int ms )
{
	Path::TimePassed(ms);
	
	if( !IsPaused() )
	{
		if (length > 0)
		{
			double distance = ((double)ms / 40.0) * velocity;
			
			if (distance > length)
				distance = length;
			
			length -= distance;
			
			if ((int)(direction & SLIDE_UP) > 0)
			{
				yDistance -= distance;
			}
			else if ((int)(direction & SLIDE_DOWN) > 0)
			{
				yDistance += distance;
			}
			else if ((int)(direction & SLIDE_RIGHT) > 0)
			{
				xDistance += distance;
			}
			else if ((int)(direction & SLIDE_LEFT) > 0)
			{
				xDistance -= distance;
			}
		}
	}
}

/**
* Check if this path collided with another path.
* 
* Overridden from Path.
* 
* @param other The other path's area.
* @param xDistance The other path's X distance.
* @param yDistance The other path's Y distance. 
* 
* @return If collided
**/
bool SlidePath::CheckCollision( Area* other, double otherX, double otherY )
{
	int side = area->Intersects(other, GetXDistance(), GetYDistance(), otherX, otherY );
	
	if( side != SIDE_NONE )
	{
		if( (int)(side & SIDE_LEFT) > 0 )
		{
			xDistance = other->GetX() + other->GetWidth() - GetX();
		}
		else if( (int)(side & SIDE_RIGHT) > 0 )
		{
			xDistance = other->GetX() - (GetX() + GetWidth());
		}
		else if( (int)(side & SIDE_BOTTOM) > 0 )
		{
			yDistance = other->GetY() - (GetY() + GetHeight()); 
		}
		else if( (int)(side & SIDE_TOP) > 0 )
		{
			yDistance = other->GetY() + other->GetHeight() - GetY();
		}
		
		length = 0;
		
		return true;
	}
	
	return false;
}

/**
* Gets if the path is still moving.
* 
* Overridden from Path.
* 
* @return If the path is moving
**/
bool SlidePath::IsMoving()
{
	return length > 0;
}
